<?php

namespace app\admin\controller;

use app\models\{Business, PlatClass, Goods, GoodsSpec, Region, User as Users, Order as Orders, OrderGoods, Address, UserAuth};
use think\Db;
use think\Validate;

class User extends Base
{

    public function index()
    {
        $data = $this->request->get();
        $results = (new Users())->order(['id' => 'desc']);
        if (!empty($data['title'])) {
            $results = $results->where('name|jl', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['level'])) {
            $results = $results->where('level', $data['level']);
        }
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $address = (new Address())->where('user_id', $result['id'])->order('is_default desc')->find() ?? [];
            $result['address'] = $address;
            $result['status_text'] = '';
            if ($result['status'] == 1) {
                $result['status_text'] = '正常';
            }
            if ($result['status'] == 2) {
                $result['status_text'] = '禁用';
            }
            $result['userauth'] = (new UserAuth())->where('id', $result['level'])->value('name') ?? '';
        }
        $userauth = (new UserAuth())->select();
        return view(null, [
            'list' => $results,
            'userauth' => $userauth,
        ]);
    }


    public function user_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'name' => 'require',
                'mobile' => 'require'
            ], [
                'name.require' => '姓名不能为空',
                'mobile.require' => '手机号不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if (!preg_match("/1[23456789]{1}\d{9}$/", $data['mobile'])) {
                return json(['code' => 1, 'data' => [], 'msg' => '请填写正确的手机号码']);
            }
            if (!empty($data['password'])) {
                $data['password'] = md5(md5($data['password']));
            }
            if (!empty($data['id'])) {
                $res = (new Users())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new Users())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 0, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new Users())->where('id', $id)->find();
            $userauth = (new UserAuth())->select();
            return view('', [
                'row' => $row,
                'userauth' => $userauth,
            ]);
        }
    }

    public function userauth()
    {
        $data = $this->request->get();
        $results = (new UserAuth())->order(['id' => 'desc']);
        if (!empty($data['title'])) {
            $results = $results->where('name', 'like', '%' . $data['title'] . '%');
        }
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        return view(null, [
            'list' => $results
        ]);
    }

    public function userauth_del()
    {
        $id = array_filter(input('id'));
        $model = new UserAuth();
        $res = $model->where(['id' => $id])->delete();
        if ($res) {
            return json(['code' => 0, 'status' => 0, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
    }

    public function userauth_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'name' => 'require',
                'zk' => 'require|gt:0|elt:100',
            ], [
                'name.require' => '等级名称不能为空',
                'zk.require' => '折扣优惠不能为空',
                'zk.gt' => '折扣优惠不能低于0',
                'zk.elt' => '折扣优惠不高于100',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            $data['zk'] = floor($data['zk'] * 100) / 100;
            if (!empty($data['id'])) {
                $res = (new UserAuth())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new UserAuth())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 0, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new UserAuth())->where('id', $id)->find();
            return view('', [
                'row' => $row
            ]);
        }
    }

    public function address()
    {
        $data = $this->request->get();
        $results = (new Address())->order(['id' => 'desc']);
        if (!empty($data['title'])) {
            $results = $results->where('name|mobile|province|city|area|address', 'like', '%' . $data['title'] . '%');
        }
        $results = $results->where('user_id', $data['id'] ?? 0)->paginate(10, false, ['query' => $this->request->param()]);
        return view(null, [
            'list' => $results,
            'id' => $data['id'] ?? 0
        ]);
    }

    public function address_del()
    {
        $id = array_filter(input('id'));
        $model = new Address();
        $res = $model->where(['id' => $id])->delete();
        if ($res) {
            return json(['code' => 0, 'status' => 0, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
    }

    public function address_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'name' => 'require',
                'mobile' => 'require',
                'user_id' => 'require',
                'province' => 'require',
                'city' => 'require',
                'area' => 'require',
                'address' => 'require',
            ], [
                'name.require' => '姓名不能为空',
                'mobile.require' => '手机号不能为空',
                'user_id.require' => '用户ID不能为空',
                'province.require' => '省份不能为空',
                'city.require' => '城市不能为空',
                'area.require' => '区域不能为空',
                'address.require' => '具体地址不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if (!empty($data['id'])) {
                $res = (new Address())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new Address())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 0, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $user_id = $this->request->get('user_id/d', 0);
            $row = (new Address())->where('id', $id)->find();
            return view('', [
                'row' => $row,
                'userid' => $user_id
            ]);
        }
    }
}
